layui.use(['jquery', 'table', 'form', 'layer'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    //使用人id
    var createId = localStorage.getItem('createId');
    //切换不同的按钮
    $('#btns').children('button').click(function () {
        $(this).removeClass('layui-white').siblings().addClass('layui-white');
    })

    //各个结果的数量
    function getResultCount(resultId, result) {
        $.ajax({
            url: '/record/recordCount',
            type: 'post',
            dataType: 'json',
            data: {'result': result, 'type': 4},
            success: function (data) {
                resultId.text(data.data);
            }
        });
    }

    //全部数量
    getResultCount($('#allCount'), "");
    //待审核数量
    getResultCount($('#toBeReviewedCount'), 2);
    //审核通过数量
    getResultCount($('#adoptCount'), 1);
    //未通过数量
    getResultCount($('#failCount'), 3);
    //全部课程
    $('#allMicrocircle').on('click', function () {
        getTable('');
    })
    //待审核课程
    $('#toBeReviewedMicrocircle').on('click', function () {
        getTable(2);
    })
    //审核通过课程
    $('#adoptMicrocircle').on('click', function () {
        getTable(1);
    })
    //未通过课程
    $('#failMicrocircle').on('click', function () {
        getTable(3);
    })
    //加载表格数据
    getTable('');

    function getTable(result) {
        $.ajax({
            url: '/record/getMicrocircleRecord',
            type: 'post',
            dataType: 'json',
            data: {'type': 4, 'result': result},
            success: function (data) {
                tablereload(data.data);
            }
        });
    }

    var tableIns;
    //表格
    tablereload = function tablereload(data) {
        tableIns = table.render({
            elem: '#table',
            id: 'table',
            data: data,
            title: '微圈审核列表',
            toolbar: '#toolbar',
            page: {
                limit: 10,
                layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                first: '首页',
                last: '尾页'
            },
            cols: [
                [
                    {type: 'checkbox'},
                    {
                        field: '', title: '序号', align: 'center', templet: function (data) {
                            return data.LAY_INDEX;
                        }
                    },
                    {field: 'microcircleId', title: '微圈编号', align: 'center'},
                    {
                        field: 'artcleUrl', title: '微圈图片', align: 'center', templet: function (data) {
                            return "<img src='http://" + data.artcleUrl + "' style='width: 100px;height: 100px'>";
                        }
                    },
                    {field: 'artcleTitle', title: '微圈标题', align: 'center'},
                    {field: 'artcleCategroyName', title: '微圈分类', align: 'center'},
                    {field: 'nickname', title: '上传人员', align: 'center'},
                    {
                        field: 'result', title: '审核状态', align: 'center', templet: function (data) {
                            if (data.result == 2) {
                                return "<p>待审核</p>";
                            } else if (data.result == 1) {
                                return "<p>审核通过</p>";
                            } else {
                                return "<p>审核未通过</p>";
                            }
                        }
                    },
                    {
                        field: '', title: '操作', align: 'center', templet: function (data) {
                            if (data.result == 2) {
                                return "<div>" +
                                    "<a href='javascript:getinformation(" + data.microcircleId + ");' style='color: #2f4056;margin-right: 10px'>查看</a>" +
                                    "<a href='javascript:updateRecord(\"" + data.id + "\",\"" + data.artcleTitle + "\");' style='color: #2f4056'>审核</a>" +
                                    "</div>";
                            }
                            return "<div>" +
                                "<a href='javascript:getinformation(" + data.microcircleId + ");' style='color: #2f4056;'>查看</a>" +
                                "</div>";

                        }
                    }
                ]
            ],
        });
    }
    //查看微圈
    getinformation = function getinformation(id) {
        localStorage.setItem('microcircleId',id);
        $.ajax({
            url: '/microcircle/getMicrocircleById',
            type: 'post',
            dataType: 'json',
            data: {'microcircleId': id, 'userId': createId},
            success: function (data) {
                viewDetails(data.data);
            }
        });
    }

    function viewDetails(data) {
        layer.open({
            type: 2,
            title: '查看微圈信息',
            shade: 0,
            area: ['1000px', '500px'],
            offset: 't',
            maxmin: true,
            btn: ['关闭'],
            content: '/microcircle/microcircle-see.html',
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                var artcleUrl = "http://"+data.artcleUrl;
                obj.find('#artcleUrl').attr('src', artcleUrl);
                obj.find('#name').text(data.nickname);
                obj.find('#createName').text(data.nickname);
                obj.find('.artcleTitle').text(data.artcleTitle);
                obj.find('#artcleCategroyName').text(data.artcleCategroyName);
                obj.find('#createDate').text(data.createDate);
                obj.find('#createAccount').text(data.account);
                obj.find('#article').html(data.article);
                obj.find('#acticleHits').text(data.acticleHits);
                obj.find('#acticleLike').text(data.acticleLike);
                obj.find('#acticleShare').text(data.acticleShare);
                obj.find('#colCount').text(data.colCount);
                var trueName = localStorage.getItem("trueName");
                obj.find('input[name=trueName]').val(trueName);
                var account = localStorage.getItem("account");
                obj.find('input[name=account]').val(account);
                obj.find('#microcircleId').val(data.microcircleId);
            }
        });
    }

    //审核
    updateRecord = function updateRecord(id, courseName) {
        layer.open({
            type: 2,
            title: '微圈审核',
            maxmin: true,
            shade: 0,
            area: ['450px', '350px'],
            content: '/examine/microcircle-examine-detailed.html',
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                obj.find('input[name=id]').val(id);
                obj.find('input[name=courseName]').val(courseName);
            }
        });
    }
    //搜索
    form.on('submit(search)',function (data) {
        $.ajax({
            url: '/record/getMicrocircleRecord?type=4',
            type: 'post',
            dataType: 'json',
            data: data.field,
            success: function (data) {
                tablereload(data.data);
            }
        });
    })
    //重置
    $('#reset').on('click',function () {
        $('input[name=artcleTitle]').val('');
        $('input[name=nickname]').val('');
        $('select[name=acticleCategroyId]').val('');
        form.render();
        getTable('');
    })
    //下拉框
    dropDownBox();

    function dropDownBox() {
        $.ajax({
            url: "/acticleCategroy/getTree",
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var d = eval(data.data);
                $('#acticleCategroyId').append("<option value=''>选择分类类型</option>");
                for (var i = 0; i < d.length - 1; i++) {
                    var id = d[i].acticleCategroyId;
                    var name = d[i].artcleCategroyName;
                    $('#acticleCategroyId').append("<option value='" + id + "'>" + name + "</option>")
                }
                form.render('select');
            }
        });
    }

    //刷新
    $('#refresh').on('click', function () {
        location.reload();
    })
    //返回
    $('#goBack').on('click', function () {
        //跳转课程列表
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    })
});